﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace DoLine.Framework.Module
{
    class TemplateHttpModule : IHttpModule
    {
        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_BeginRequest);
            context.EndRequest += new EventHandler(Application_EndRequest);

        }

        public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            string url = context.Request.Path;//获取当前的url路径
            //String AppPath = context.Request.ApplicationPath;//应用程序的路径
            
            if (url=="/")
            {
                url = "index.html";
            }
            if (url.ToLower().Contains(".html") && !url.ToLower().Contains("admin"))
            {
                string currentTemp = GetTemplage();
                context.Server.Transfer("/templates/" + currentTemp + url.Replace("html", "aspx"));
            }
            else
            {
                HttpResponse response = context.Response;
            }
            
            //response.Write("这个是TemplateHttpModule中有BeginRequest");
        }

        /// <summary>
        /// 该方法是获取当前的模版 途径 可以是从数据库中 或者 从配置中 或者缓存中
        /// </summary>
        /// <returns></returns>
        private string GetTemplage()
        {
            return "default";
        }

        public void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            HttpResponse response = context.Response;
            //response.Write("这是TemplateHttpModule中有EndRequest");
        }


    }

}
